package com.worldcup.service.question.calculator;

import com.worldcup.model.question.QuestionUserAnswer;
import com.worldcup.service.question.QuestionMatchResultService;
import com.worldcup.service.question.QuestionOptionService;
import com.worldcup.service.question.QuestionService;
import com.worldcup.service.question.impl.QuestionUserAnswerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author duwan.zq
 * @date 2019/03/12
 */
@Service
public class MatchResultTaskPool {

    private final static Logger logger = LoggerFactory.getLogger(MatchResultTaskPool.class);

    private ExecutorService userExecutor;

    private ExecutorService userAnswerExecutor;

    @Autowired
    private QuestionUserAnswerService userAnswerService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionOptionService optionService;

    @Autowired
    private QuestionMatchResultService matchResultService;

    @PostConstruct
    public void init(){
        userExecutor = new ThreadPoolExecutor(5, 5, 10000000L,TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(300));
        userAnswerExecutor = new ThreadPoolExecutor(20, 20, 10000000L,TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(300));


    }

    @PreDestroy
    public void destroy(){
        userExecutor.shutdown();
        userAnswerExecutor.shutdown();
    }

    public void submitUserTask(String openid, int priority) {
        this.userExecutor.execute(() -> {
            Thread.currentThread().setName("user-" + openid);
            Thread.currentThread().setPriority(priority);
            logger.info("提交user任务, openid = {}", openid);
            List<QuestionUserAnswer> answers = userAnswerService.getAnswers(openid);
            //获取所有用户
            List<String> passiveOpenids = userAnswerService.getAllUser();
            for(String passiveOpenid : passiveOpenids){
                if(openid.equals(passiveOpenid)){
                    continue;
                }
                List<QuestionUserAnswer> passiveAnswers = userAnswerService.getAnswers(passiveOpenid);
                userAnswerExecutor.execute(new MatchResultTask(answers, passiveAnswers, questionService, optionService, matchResultService, priority));
            }

        });
    }

    public void submitUserTask(String openid, String passiveOpenid, int priority) {
        this.userExecutor.execute(() -> {
            Thread.currentThread().setName("user-" + openid);
            Thread.currentThread().setPriority(priority);
            logger.info("提交user任务, openid = {}, passiveOpenid = {}", openid, passiveOpenid);
            List<QuestionUserAnswer> answers = userAnswerService.getAnswers(openid);
            List<QuestionUserAnswer> passiveAnswers = userAnswerService.getAnswers(passiveOpenid);
            userAnswerExecutor.execute(new MatchResultTask(answers, passiveAnswers, questionService, optionService, matchResultService, priority));
        });
    }

    public void runAllUserTAsk(){
        //this.userExecutor.execute(() -> {
        //    List<String> openids = userAnswerService.getAllUser();
        //    for(String openid : openids){
        //
        //        List<QuestionUserAnswer> answers = userAnswerService.getAnswers(openid);
        //        //获取所有用户
        //        List<String> passiveOpenids = userAnswerService.getAllUser();
        //        for(String passiveOpenid : passiveOpenids){
        //            if(openid.equals(passiveOpenid)){
        //                continue;
        //            }
        //            List<QuestionUserAnswer> passiveAnswers = userAnswerService.getAnswers(passiveOpenid);
        //            new MatchResultTask(answers, passiveAnswers, questionService, optionService, matchResultService, 5).run();
        //        }
        //    }
        //});
        List<String> openids = userAnswerService.getAllUser();
        for(String openid : openids){
            this.submitUserTask(openid, Thread.NORM_PRIORITY);
        }
    }
}
